﻿import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Window 2.15

import "." // 导入项目的根目录

Window {
    id: i2cPage
    visible: true
    width: 1200
    height: 768
    minimumWidth: 1200 // 设置最小宽度
    minimumHeight: 768 // 设置最小高度
    title: "I2C Test"

    // 这个函数是模拟获取I2C状态的函数，实际应用中需要替换为真实的获取状态逻辑
    function getI2CStatus() {
        // 模拟I2C状态数据，这里只是随机生成一个字符串作为示例
        var status = "数据更新" + Math.floor(Math.random() * 100);
        return status;
    }

    // 定时器，模拟I2C数据更新
    Timer {
        id: i2cTimer
        interval: 1000 // 更新间隔，单位为毫秒
        repeat: true   // 是否重复执行
        onTriggered: {
            // 这里是模拟I2C数据更新的地方
            // 假设我们有一个函数getI2CStatus()来获取I2C的状态
            var i2cStatus = getI2CStatus();

            // 更新文本显示框的内容
            i2cDataDisplay.text = "I2C状态: " + i2cStatus;
        }
    }

    Item {
        id: i2cItem
        anchors.fill : parent

        ColumnLayout {
            id: i2cColumn
            anchors.left : i2cItem.left
            height       : i2cItem.height
            width        : i2cItem.width * 0.90

            // 上半部分区域
            Rectangle {
                id:i2cTop
                width: i2cColumn.width * 0.99
                height: i2cColumn.height * 0.60
                anchors.top: i2cColumn.top
                anchors.topMargin: 3
                anchors.left: parent.left
                anchors.leftMargin: 3
                border.color: "#c0c0c0"     // 边框颜色
                border.width: 2             // 边框宽度

                color: "#f0f0f0"

                // 上半部分第一个框
                Rectangle {
                    id: i2cSetup
                    width: i2cTop.width * 0.99
                    height: i2cTop.height * 0.49
                    anchors.top: i2cTop.top
                    anchors.topMargin: 6
                    anchors.left: i2cTop.left
                    anchors.leftMargin: 6

                    ColumnLayout {
                        anchors.centerIn: parent
                        spacing: 10

                        RowLayout {
                            id: i2cTestGroup0
                            //anchors.fill: parent
                            width: i2cTop.width * 0.99
                            height: i2cTop.height * 0.10
                            anchors.top: i2cTop.top
                            anchors.topMargin: 5
                            anchors.left: i2cTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: i2cTestGroup0Text
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "测试程序选择:"
                            }

                            TextField {
                                id: i2cTestGroup0Input
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "I2C test"
                            }

                            Text {
                                id: i2cTestGroup0Interface
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "应用层接口:"
                            }

                            TextField {
                                id: i2cTestGroup0InterfaceInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "/dev/i2c-0"
                            }


                            Text {
                                id: i2cTestGroup0frent
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "循环次数:"
                            }

                            TextField {
                                id: i2cTestGroup0frentInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "100"
                            }

                            Button {
                                id: clearDatabutton0
                                text: "测 试"
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                onClicked: {
                                    console.log("i2c 测试 接口 0")
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    i2ctest.sendRemoteCommand(host, user, i2cTestGroup0Input.text);
                                }
                            }
                        }

                        RowLayout {
                            id: i2cTestGroup1
                            width: i2cTop.width * 1
                            height: i2cTop.height * 0.1
                            anchors.top: i2cTestGroup0.bottom
                            anchors.topMargin: 5
                            anchors.left: i2cTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: i2cTestGroup1Text
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "测试程序选择:"
                            }

                            TextField {
                                id: i2cTestGroup1Input
                                width: i2cTop.width * 0.25
                                placeholderText: "I2C test"
                            }

                            Text {
                                id: i2cTestGroup1Interface
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "应用层接口:"
                            }

                            TextField {
                                id: i2cTestGroup1InterfaceInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "/dev/i2c-0"
                            }


                            Text {
                                id: i2cTestGroup1frent
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "循环次数:"
                            }

                            TextField {
                                id: i2cTestGroup1frentInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "100"
                            }

                            Button {
                                id: clearDatabutton1
                                text: "测 试"
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                onClicked: {
                                    console.log("i2c 测试 接口 1")
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    i2ctest.sendRemoteCommand(host, user, i2cTestGroup1Input.text);
                                }
                            }
                        }

                        RowLayout {
                            id: i2cTestGroup2
                            width: i2cTop.width * 1
                            height: i2cTop.height * 0.1
                            anchors.top: i2cTestGroup1.bottom
                            anchors.topMargin: 5
                            anchors.left: i2cTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: i2cTestGroup2Text
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "测试程序选择:"
                            }

                            TextField {
                                id: i2cTestGroup2Input
                                width: i2cTop.width * 0.25
                                placeholderText: "I2C test"
                            }

                            Text {
                                id: i2cTestGroup2Interface
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "应用层接口:"
                            }

                            TextField {
                                id: i2cTestGroup2InterfaceInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "/dev/i2c-0"
                            }


                            Text {
                                id: i2cTestGroup2frent
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "循环次数:"
                            }

                            TextField {
                                id: i2cTestGroup2frentInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "100"
                            }

                            Button {
                                id: clearDatabutton2
                                text: "测 试"
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                onClicked: {
                                    console.log("i2c 测试 接口 1")
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    i2ctest.sendRemoteCommand(host, user, i2cTestGroup2Input.text);
                                }
                            }
                        }

                        RowLayout {
                            id: i2cTestGroup3
                            width: i2cTop.width * 1
                            height: i2cTop.height * 0.1
                            anchors.top: i2cTestGroup2.bottom
                            anchors.topMargin: 5
                            anchors.left: i2cTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: i2cTestGroup3Text
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "测试程序选择:"
                            }

                            TextField {
                                id: i2cTestGroup3Input
                                width: i2cTop.width * 0.25
                                placeholderText: "I2C test"
                            }

                            Text {
                                id: i2cTestGroup3Interface
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "应用层接口:"
                            }

                            TextField {
                                id: i2cTestGroup3InterfaceInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "/dev/i2c-0"
                            }


                            Text {
                                id: i2cTestGroup3frent
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "循环次数:"
                            }

                            TextField {
                                id: i2cTestGroup3frentInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "100"
                            }

                            Button {
                                id: clearDatabutton3
                                text: "测 试"
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                onClicked: {
                                    console.log("i2c 测试 接口 3")
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    i2ctest.sendRemoteCommand(host, user, i2cTestGroup3Input.text);
                                }
                            }
                        }
                    }
                }

                // 上半部分第二个框
                Rectangle {
                    id: i2cTopStatus
                    width: i2cTop.width * 0.99
                    height: i2cTop.height * 0.48
                    anchors.top: i2cSetup.bottom
                    anchors.topMargin: 4
                    anchors.left: i2cTop.left
                    anchors.leftMargin: 6

                    ColumnLayout {
                        anchors.centerIn: parent
                        spacing: 10

                        RowLayout {
                            id: i2cTestGroup4
                            //anchors.fill: parent
                            width: i2cTop.width * 0.99
                            height: i2cTop.height * 0.10
                            anchors.top: i2cTopStatus.top
                            anchors.topMargin: 5
                            anchors.left: i2cTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: i2cTestGroup4Text
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "测试程序选择:"
                            }

                            TextField {
                                id: i2cTestGroup4Input
                                width: i2cTop.width * 0.25
                                height: i2cSetup.height * 0.15
                                placeholderText: "I2C test"
                            }

                            Text {
                                id: i2cTestGroup4Interface
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "应用层接口:"
                            }

                            TextField {
                                id: i2cTestGroup4InterfaceInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "/dev/i2c-0"
                            }


                            Text {
                                id: i2cTestGroup4frent
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "循环次数:"
                            }

                            TextField {
                                id: i2cTestGroup4frentInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "100"
                            }

                            Button {
                                id: clearDatabutton4
                                text: "测 试"
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                onClicked: {
                                    console.log("i2c 测试 接口 4")
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    i2ctest.sendRemoteCommand(host, user, i2cTestGroup4Input.text);
                                }
                            }
                        }

                        RowLayout {
                            id: i2cTestGroup5
                            //anchors.fill: parent
                            width: i2cTop.width * 0.99
                            height: i2cTop.height * 0.10
                            anchors.top: i2cTopStatus.top
                            anchors.topMargin: 5
                            anchors.left: i2cTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: i2cTestGroup5Text
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "测试程序选择:"
                            }

                            TextField {
                                id: i2cTestGroup5Input
                                width: i2cTop.width * 0.25
                                height: i2cSetup.height * 0.15
                                placeholderText: "I2C test"
                            }

                            Text {
                                id: i2cTestGroup5Interface
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "应用层接口:"
                            }

                            TextField {
                                id: i2cTestGroup5InterfaceInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "/dev/i2c-0"
                            }


                            Text {
                                id: i2cTestGroup5frent
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "循环次数:"
                            }

                            TextField {
                                id: i2cTestGroup5frentInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "100"
                            }

                            Button {
                                id: clearDatabutton5
                                text: "测 试"
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                onClicked: {
                                    console.log("i2c 测试 接口 5")
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    i2ctest.sendRemoteCommand(host, user, i2cTestGroup5Input.text);
                                }
                            }
                        }

                        RowLayout {
                            id: i2cTestGroup6
                            //anchors.fill: parent
                            width: i2cTop.width * 0.99
                            height: i2cTop.height * 0.10
                            anchors.top: i2cTopStatus.top
                            anchors.topMargin: 5
                            anchors.left: i2cTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: i2cTestGroup6Text
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "测试程序选择:"
                            }

                            TextField {
                                id: i2cTestGroup6Input
                                width: i2cTop.width * 0.25
                                height: i2cSetup.height * 0.15
                                placeholderText: "I2C test"
                            }

                            Text {
                                id: i2cTestGroup6Interface
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "应用层接口:"
                            }

                            TextField {
                                id: i2cTestGroup6InterfaceInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "/dev/i2c-0"
                            }


                            Text {
                                id: i2cTestGroup6frent
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "循环次数:"
                            }

                            TextField {
                                id: i2cTestGroup6frentInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "100"
                            }

                            Button {
                                id: clearDatabutton6
                                text: "测 试"
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                onClicked: {
                                    console.log("i2c 测试 接口 6");
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    i2ctest.sendRemoteCommand(host, user, i2cTestGroup6Input.text);
                                }
                            }
                        }

                        RowLayout {
                            id: i2cTestGroup7
                            //anchors.fill: parent
                            width: i2cTop.width * 0.99
                            height: i2cTop.height * 0.10
                            anchors.top: i2cTopStatus.top
                            anchors.topMargin: 5
                            anchors.left: i2cTop.left
                            anchors.leftMargin: 5
                            spacing: 15

                            Text {
                                id: i2cTestGroup7Text
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "测试程序选择:"
                            }

                            TextField {
                                id: i2cTestGroup7Input
                                width: i2cTop.width * 0.25
                                height: i2cSetup.height * 0.15
                                placeholderText: "I2C test"
                            }

                            Text {
                                id: i2cTestGroup7Interface
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "应用层接口:"
                            }

                            TextField {
                                id: i2cTestGroup7InterfaceInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "/dev/i2c-0"
                            }


                            Text {
                                id: i2cTestGroup7frent
                                width: i2cTop.width * 0.2
                                anchors.left: i2cTop.left
                                anchors.leftMargin: 5
                                text: "循环次数:"
                            }

                            TextField {
                                id: i2cTestGroup7frentInput
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                placeholderText: "100"
                            }

                            Button {
                                id: clearDatabutton7
                                text: "测 试"
                                width: i2cTop.width * 0.20
                                height: i2cSetup.height * 0.15
                                onClicked: {
                                    console.log("i2c 测试 接口 7");
                                    var host = backend.host;
                                    var user = backend.user;
                                    var command ="/data/ZhangDong/DAS1/APP/cpp/PrintTime/build/PrintTime";
                                    i2ctest.sendRemoteCommand(host, user, i2cTestGroup7Input.text);
                                }
                            }
                        }
                    }
                }
            }

            // 错误信息显示
            Text {
                id: i2cLogText
                color: "blue"
                anchors.top: i2cTop.bottom
                anchors.topMargin: 2
                anchors.left: i2cTop.left
                anchors.leftMargin: 10
                text: "输出日志:"
            }

            // 下半部分 显示数据
            Rectangle {
                id: i2cStatusDisplay
                width : i2cColumn.width  * 0.99
                height: i2cColumn.height * 0.35
                anchors.left: i2cTop.left
                anchors.top: i2cLogText.bottom
                anchors.topMargin: 4
                color: "#f0f0f0"
                border.color: "#c0c0c0"     // 边框颜色
                border.width: 2             // 边框宽度

                ScrollView{
                   id: scrollView
                   anchors.fill: parent
                   clip: true

                   // 文本显示框
                    TextArea {
                        id: i2cDataDisplay
                        anchors.fill: parent // 让 TextArea 填充整个 ScrollView
                        readOnly: true       // 使其只读，不可编辑
                        placeholderText: "接收到的数据"
                        color: "black"       // 设置字体颜色为黑色
                        background: Rectangle {
                            color: "transparent" // 背景透明，显示父元素的颜色
                        }
                        wrapMode: TextArea.Wrap // 自动换行
                        font.pixelSize: 14      // 字体大小（可以根据需要调整）
                    }
                }
            }

            // 该信号连接和注册方式，更加简单好用
            Connections {
                target: i2ctest
                onProcessFinished: {
                    console.log("Process finished!")
                }
                onCommandOutput: {
                    // 获取当前时间，并格式化为日志记录的格式
                    var time = Qt.formatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss") // 格式化为: "2024-12-18 11:22:30"

                    // 将时间和输出内容一起追加到文本框,append会自动让文本显示在最新位置
                    i2cDataDisplay.append("[" +time + "] " + output)
                }
            }

            // 错误信息显示
            Text {
                id: errorDisplay
                color: "red"
                anchors.top: i2cStatusDisplay.bottom
                anchors.topMargin: 2
                anchors.left: i2cTop.left
                anchors.leftMargin: 10
                text: "未连接......"
            }
        }

        Rectangle {
            id: i2cInfo
            anchors.right: i2cItem.right
            height       : i2cItem.height * 0.97
            width        : i2cItem.width * 0.09
            anchors.top: i2cItem.top
            anchors.topMargin: 3
            anchors.rightMargin: 10
            border.color: "#c0c0c0"     // 边框颜色
            border.width: 2             // 边框宽度

            Button {
                id: i2cExitButton
                text: "返 回"
                width: i2cInfo.width * 0.80
                height: i2cInfo.height * 0.05
                anchors.left: i2cInfo.left
                anchors.leftMargin: i2cInfo.width * 0.1
                anchors.top: i2cInfo.top
                anchors.topMargin: 5
                onClicked: {
                    console.log("返回主界面")
                    i2cPage.visible = false
                    mainPage.visible = true
                }
            }

            Button {
                id: i2cClearButton
                text: "清 除"
                width: i2cInfo.width * 0.80
                height: i2cInfo.height * 0.05
                anchors.left: i2cInfo.left
                anchors.leftMargin: i2cInfo.width * 0.1
                anchors.top: i2cInfo.top
                anchors.topMargin: i2cInfo.height * 0.65
                onClicked: {
                    console.log("清除i2c文本框信息")
                    i2cDataDisplay.clear()
                }
            }
        }
    }
}
